import Vue from 'vue'
import router from './router'
import App from './App.vue'
import ElementUI, {MessageBox} from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './plugins/element'
import TreeTable from 'vue-table-with-tree-grid'
import './assets/css/global.css'
import './assets/fonts/iconfont.css'
import axios from "axios";

import VueQuillEditor from 'vue-quill-editor'

import 'quill/dist/quill.core.css' // 引入样式
import 'quill/dist/quill.snow.css' // snow theme
import 'quill/dist/quill.bubble.css' // bubble theme

import {Quill} from 'vue-quill-editor'

window.Quill = Quill
const scriptEl = document.createElement('script');
scriptEl.charset = 'utf-8'
scriptEl.src = './plugins/image-resize.min.js'
const head = document.head || document.getElementsByTagName('head')[0];
head.appendChild(scriptEl);


Vue.use(VueQuillEditor, /* { default global options } */)

Vue.prototype.$axios = axios

//手动注册
Vue.component('tree-table', TreeTable)
// Vue.prototype.$http = axios

// axios请求头设置
axios.interceptors.request.use(config => {
    if (window.sessionStorage.getItem("access_token")) {
        config.headers.Authorization = "Bearer " + window.sessionStorage.getItem("access_token");
    }
    return config;
})

// 拦截错误请求
axios.interceptors.response.use(
    (response) => {
        // console.log(response);
        if (response.data.code != null && response.data.code !== 200) {
            ElementUI.Message({
                message: response.data.message,
                type: "error",
                showClose: true,
            });
        }
        return response;
    },
    (error) => {
        if (error.response) {
            if (error.response.status === 403) {
                MessageBox.alert(
                    "抱歉！您的身份认证信息已过期！请重新登录！",
                    "身份认证过期",
                    {type: "error"}
                ).then(() => {
                    router.push("/login");
                })
            } else if (error.response.status === 400) {
                MessageBox.alert("出错啦！请联系管理员", "错误！", {
                    type: "error",
                });
            } else if (error.response.status === 500) {
                ElementUI.Message({
                    message: error.response.statusText + "，请尝试刷新重试",
                    type: "error",
                    duration: 5000,
                    showClose: true,
                });
            }
        }
        return Promise.reject(error.response.data);
    }
);

// 处理跨域问题
Vue.prototype.HOST = "/api";
Vue.prototype.imgURL = "http://ich.image.relosy.cn/";

//取消 Vue 所有的日志与警告。
Vue.config.silent = true
//设置为 false 以阻止 vue 在启动时生成生产提示。
Vue.config.productionTip = false;


Vue.use(ElementUI)


Vue.prototype.$setgoindex = function () {
    if (window.history.length <= 1) {
        if (location.href.indexOf('?') === -1) {
            window.location.href = location.href + '?goindex=true'
        } else if (location.href.indexOf('?') !== -1 && location.href.indexOf('goindex') === -1) {
            window.location.href = location.href + '&goindex=true'
        }
    }
}


new Vue({
    router,
    render: h => h(App),
}).$mount('#app')
